﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using LDSP.BRL;
using System.IO;
using BYFW.SFL;
using BYFW.BOL;

namespace LDSP.Util
{
    public class CommonService
    {
        static CommonService()
        {
            TimerService.Elapsed += new ElapsedEventHandler(TimerService_Elapsed);
        }

        static void TimerService_Elapsed(object sender, ElapsedEventArgs e)
        {
            s_enterTimer = true;
            TimerService.Stop();
            lock (s_lock)
            {
                try
                {
                    DoTimerEvent();
                }
                catch { }
                finally
                {
                    TimerService.Start();
                    s_enterTimer = false;
                }
            }
        }

        public static Timer TimerService = new Timer();
        private static bool s_enterTimer = false;
        public static string SITEROOT = "";

        private static object s_lock = new object();

        public static void CheckTimerService()
        {
            if (!TimerService.Enabled && !s_enterTimer)
            {
                TimerService.Interval = 1000 * 60 * 10;
                TimerService.AutoReset = true;
                TimerService.Start();
            }
        }

        protected static void DoTimerEvent()
        {
            ConvertCodificationPDFFileToDB();
            ConvertAccidentCasePDFFileToDB();
            ConvertNewsPDFFileToDB();
        }

        public static void ConvertCodificationPDFFileToDB()
        {
            Action act = new Action(delegate()
            {
                lock (s_lock)
                {
                    CheckTimerService();
                    Codification ett = new Codification();
                    string cond = "where isConverted=0 or isConverted is null";
                    var list = ett.GetSpecialList<Codification>(null, cond);
                    foreach (var item in list)
                    {
                        try
                        {
                            string fn = item.GetDownloadFileFullName();
                            if (File.Exists(fn))
                            {
                                item.IsConverted = true;
                                if (item.FileExtension.Trim().ToLower().EndsWith(".pdf"))
                                {
                                    item.PDFContent = PDFOP.ParseText(fn);
                                }
                                item.Update();
                            }
                        }
                        catch (Exception ex)
                        {
                            string msg = string.Format("转换{0}【{1}】的文件[{2}]失败\r\n{3}",
                                item.DocType.ToString(),
                                item.Name,
                                item.FileExtension,
                                ex.ToString());
                            LogError(msg);
                        }
                    }
                }
            });
            act.BeginInvoke(null, null);
        }

        public static void ConvertAccidentCasePDFFileToDB()
        {
            Action act = new Action(delegate()
            {
                lock (s_lock)
                {
                    CheckTimerService();
                    var ett = new AccidentCase();
                    string cond = "where isConverted=0 or isConverted is null";
                    var list = ett.GetSpecialList<AccidentCase>(null, cond);
                    foreach (var item in list)
                    {
                        try
                        {
                            string fn = item.GetDownloadFileFullName();
                            if (File.Exists(fn))
                            {
                                item.IsConverted = true;
                                if (item.FileExtension.Trim().ToLower().EndsWith(".pdf"))
                                {
                                    item.PDFContent = PDFOP.ParseText(fn);
                                }
                                item.Update();
                            }
                        }
                        catch (Exception ex)
                        {
                            string msg = string.Format("转换{0}【{1}】的文件[{2}]失败\r\n{3}",
                                DocType.事故案例.ToString(),
                                item.Name,
                                item.FileExtension,
                                ex.ToString());
                            LogError(msg);
                        }
                    }
                }
            });
            act.BeginInvoke(null, null);
        }

        public static void ConvertNewsPDFFileToDB()
        {
            Action act = new Action(delegate()
            {
                lock (s_lock)
                {
                    CheckTimerService();
                    var ett = new News();
                    string cond = "where isConverted=0 or isConverted is null";
                    var list = ett.GetSpecialList<News>(null, cond);
                    foreach (var item in list)
                    {
                        try
                        {
                            string fn = item.GetDownloadFileFullName();
                            if (File.Exists(fn))
                            {
                                item.IsConverted = true;
                                if (item.FileExtension.Trim().ToLower().EndsWith(".pdf"))
                                {
                                    item.PDFContent = PDFOP.ParseText(fn);
                                }
                                item.Update();
                            }
                        }
                        catch (Exception ex)
                        {
                            string msg = string.Format("转换{0}【{1}】的文件[{2}]失败\r\n{3}",
                                DocType.新闻.ToString(),
                                item.Name,
                                item.FileExtension,
                                ex.ToString());
                            LogError(msg);
                        }
                    }
                }
            });
            act.BeginInvoke(null, null);
        }


        public static void LogError(string msg)
        {
            LogOP.Log.LogError(msg);
        }
    }
}
